package net.lzzy.spingsecurity.config;

import net.lzzy.spingsecurity.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
//    @Override
//    protected void configure(AuthenticationManagerBuilder auth) throws
//            Exception {
//        BCryptPasswordEncoder bCryptPasswordEncoder = new
//                BCryptPasswordEncoder();
//        auth.inMemoryAuthentication().passwordEncoder(bCryptPasswordEncoder).
//                withUser("common").password(bCryptPasswordEncoder.encode("123")).roles("common")
//                .and().withUser("vip").password(bCryptPasswordEncoder.encode("123")).roles("vip").
//                roles("common");
    @Autowired
    public  UserService userService;
//    }
@Override
protected void configure(AuthenticationManagerBuilder auth)throws
        Exception {
    BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
    auth.userDetailsService(userService).passwordEncoder(encoder);
}

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/**").permitAll()
                .antMatchers("/common/**").hasRole("custom")
                .antMatchers("/vip/**").hasRole("admin")
                .anyRequest().authenticated();
        http.formLogin()
                .loginPage("/login").permitAll()
                .usernameParameter("username")
                .passwordParameter("password")

                .defaultSuccessUrl("/index")
                .failureUrl("/login?error");
        http.logout()
                .logoutUrl("/logout")
                .logoutSuccessUrl("/index");
        http.rememberMe()
                .rememberMeParameter("rememberme")
                .tokenValiditySeconds(200);
    }
}

